/*
** map.c for zappy in /home/morgan_t/Documents/svn-for-my-proj/zappy/server/src
** 
** Made by thomas morgant
** Login   <morgan_t@epitech.net>
** 
** Started on  Thu Jul 12 14:27:12 2012 thomas morgant
** Last update Thu Jul 12 16:08:37 2012 thomas morgant
*/

#include "common.h"
#include "server.h"

void	load_elem(t_map **map, int width, int height, int elem)
{
  t_map *temp;

  temp = xmalloc(sizeof(*temp));
  create_map(temp);
  temp->width = width;
  temp->height = height;
  temp->tab[elem]++;
  temp->next = *map;
  *map = temp;
}

int	check_case(t_map *map, int width, int height, int elem)
{
  t_map *temp;

  temp = map;
  while (temp)
    {
      if ((temp->width == width) && (temp->height == height))
	{
	  temp->tab[elem]++;
	  return (0);
	}
      temp = temp->next;
    }
  return (-1);
}

int	*get_elem_little_map(t_serv *serv, int *elem)
{
  int	x;

  x = serv->client_in_team * 2;
  elem[0] = (5 * x);
  elem[1] = (2 * x);
  elem[2] = (2 * x);
  elem[3] = (3 * x);
  elem[4] = (3 * x);
  elem[5] = (2 * x);
  elem[6] = (x);
  return (elem);
}

int	*get_elem(t_serv *serv, int *elem)
{
  int	x;
  int	y;
  
  x = serv->client_in_team * 2;
  y = (serv->width * serv->height) / 100; 
  if ((serv->width * serv->height) >= 100)
    {
      elem[0] = (5 * x * y);
      elem[1] = (2 * x * y);
      elem[2] = (2 * x * y);
      elem[3] = (3 * x * y);
      elem[4] = (3 * x * y);
      elem[5] = (2 * x * y);
      elem[6] = (x * y);
    }
  else
    get_elem_little_map(serv, elem);
  return (elem);
}

void	map(t_serv *serv)
{
  int	elem[7];
  int	i;
  int	width;
  int	height;

  i = 0;
  serv->map = 0;
  srand(time(0));
  get_elem(serv, elem);
  while (i < 7)
    {
      if (elem[i] > 0)
	{
	  width = rand() % serv->width;
	  height = rand() % serv->height;
	  if (check_case(serv->map, width, height, i) < 0)
	    load_elem(&serv->map, width, height, i);
	}
      if (elem[i] -- <= 0)
	i++;
    }
}
